package auctiongui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import auctioncentral.AbstractUser;
import auctioncentral.AuctionSystem;
import auctioncentral.Bid;
import auctioncentral.Item;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * A popup that encapsulates the behavior of adding new items and bidding on those 
 * items (from the user views of NonProfit and Bidder respectively).
 *
 * @author Keenan Fejeran
 * @version 3/10/2014
 */
public class NewItem extends javax.swing.JFrame {
	
	private String my_event_name;
	
	private Item my_item;

	private String Title;
	
	private AuctionSystem my_system;
	
	private AbstractUser my_bidder;
	
	private JTable my_upper_frame;
	
	//private int global_int_id;

    /**
     * Creates new form NewItem for a bidder viewer.
     * 
     */
    public NewItem(AuctionSystem the_system, String the_event_name, Item the_item, AbstractUser the_user, JTable jTable2) {
    	my_event_name = the_event_name;
    	my_system = the_system;
    	my_item = the_item;
    	my_bidder = the_user;
    	Title = "Bid";
    	my_upper_frame = jTable2;
        initComponents();
        setup();
        
        //Bidder specific window modifiers.
        jButton1.setVisible(false);
        //jButton1.setEnabled(false);
        jButton3.setText("Close");
        jTextArea1.setEditable(false);
        jTextArea2.setEditable(false);
        
    }
	
    /**
     * Creates new form NewItem for a nonprofit viewer.
     * 
     */
    public NewItem(AuctionSystem the_system, String the_event_name, Item the_item, JTable upper_frame) {
    	my_event_name = the_event_name;
    	my_system = the_system;
    	my_item = the_item;
    	Title = "Bid";
    	my_upper_frame = upper_frame;
        initComponents();
        setup();
        jButton2.setVisible(false);
        jTextField8.setVisible(false);
        jLabel10.setVisible(false);
        
    }
    
    //Constructor for the auction central view.
    public NewItem(AuctionSystem the_system, String the_event_name, JTable jTable2) {
    	my_system = the_system;
    	my_event_name = the_event_name;
    	my_item = null;
    	Title = "Item Information";
    	my_upper_frame = jTable2;
        initComponents();
        setup();
        
        jButton2.setVisible(false);
        jTextField8.setVisible(false);
        jLabel10.setVisible(false);
        
        //jButton1.setVisible(false);
        //jButton1.setEnabled(false);
        jButton3.setText("Close");
    }
    
    public void setup() {
    	jButton3.addActionListener(new CancelListener(this));
    	
    	jButton2.addActionListener(new BidListener());
    	
    	jButton1.addActionListener(new AddItemListener());
    	
    	jTextField1.setText(my_event_name);
    	jTextField1.setEnabled(false);
    	
    	if (my_item != null) {
    		jTextField2.setText(my_item.getName());
    		jTextField3.setText("" + my_item.getQuantity());
    		jTextField4.setText("" + my_item.getMy_min_starting_bid());
    		jTextField5.setText(my_item.getMy_donor());
    		jTextField6.setText(my_item.getMy_size());
    		jTextField7.setText(my_item.getMy_storage());
    		//jTextField8.setText(my_item.getMy_condition());
    		jTextArea1.setText(my_item.getMy_condition());
    		jTextArea2.setText(my_item.getMy_comments());
    		
    		JTextField[] textArray = {jTextField2, jTextField3, jTextField4, jTextField5, jTextField6, jTextField7};
    		for (JTextField j: textArray) {
    			j.setEnabled(false);
    		}
    	}
    }
    
    public class CancelListener implements ActionListener {

    	NewItem my_new_item;
    	
    	public CancelListener(NewItem the_new_item) {
    		my_new_item = the_new_item;
    	}
    	
        public void actionPerformed(ActionEvent e)
        {
        	my_upper_frame.setModel(new TableModel(my_system.findEventFromName(my_event_name).getItems()));
        	my_upper_frame.validate();
			my_upper_frame.repaint();
          my_new_item.setVisible(false);
          my_new_item.dispose();
          
        }
    	
    }
    
    public class AddItemListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			int itemID = my_system.addItem(new Item(jTextField2.getText(), Integer.parseInt(jTextField3.getText()), my_event_name, Integer.parseInt(jTextField4.getText()), jTextField5.getText(),
				      jTextField6.getText(), jTextField7.getText(), jTextArea1.getText(), jTextArea2.getText()));
			my_system.findEventFromName(my_event_name).addItems(new Item(jTextField2.getText(), Integer.parseInt(jTextField3.getText()), my_event_name, Integer.parseInt(jTextField4.getText()), jTextField5.getText(),
				      jTextField6.getText(), jTextField7.getText(), jTextArea1.getText(), jTextArea2.getText()));
//			my_system.findEventFromName(my_event_name).getItems().add(new Item(jTextField2.getText(), Integer.parseInt(jTextField3.getText()), my_event_name, Integer.parseInt(jTextField4.getText()), jTextField5.getText(),
//				      jTextField6.getText(), jTextField7.getText(), jTextArea1.getText(), jTextArea2.getText()));
			
			
			JOptionPane.showMessageDialog(null, "Item Added, ID# = " +itemID);
			my_upper_frame.setModel(new TableModel(my_system.findEventFromName(my_event_name).getItems()));
        	my_upper_frame.validate();
			my_upper_frame.repaint();
		}
    	
    }
    
    public class BidListener implements ActionListener {
    	
        public void actionPerformed(ActionEvent e) {
        	
        	//Do nothing unless an amount is entered
        	if (jTextField8.getText().length() > 0) {
            	
            	Double amountBid = Double.parseDouble(jTextField8.getText());
            	
            	//Business rules checks for placing bids:
            	List<Bid> usersBids = my_system.findBidsFromUser(my_bidder.getName());
            	
            	boolean bidExists = false;
            	
            	if (usersBids != null) {
                	for (Bid b: usersBids) {
                		if (b.getItemId() == my_item.getMy_id()){
                			bidExists = true;
                		}
                	}

            	}
            	
            	if (amountBid >= my_item.getMy_min_starting_bid() && !bidExists) {
            		my_system.addBid(new Bid(amountBid, my_bidder.getName(), my_item.getMy_id()));
            		JOptionPane.showMessageDialog(null, "Bid Placed.");
            	} else {
            		JOptionPane.showMessageDialog(null, "Sorry, your bid is too low or\nyou already have a bid on this item.", "Error", JOptionPane.ERROR_MESSAGE);
            	}
            	
        	} else {
        		JOptionPane.showMessageDialog(null, "Please enter an amount to bid.", "Error", JOptionPane.ERROR_MESSAGE);
        	}

        } //end action performed.
    	
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

    	setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    	setResizable(false);
    	jPanel1 = new javax.swing.JPanel();
        titleLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField6 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        titleLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titleLabel.setText(Title);
        titleLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jLabel1.setText("Auction Name");

        jLabel2.setText("Item Name");

        jLabel3.setText("Item Quantity");

        jLabel4.setText("Minimum Starting Bid");

        jLabel5.setText("Donor");

        jLabel6.setText("Size");

        jLabel7.setText("Storage");

        jLabel8.setText("Condition");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel9.setText("Comments");

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jLabel10.setText("Amount ($.$$)");

        jButton2.setText("Bid");

        jButton1.setText("Save Changes");

        jButton3.setText("Cancel");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(153, 153, 153)
                                .addComponent(titleLabel))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel9)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField3))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField4))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField2))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField1))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField5))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField6))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField7))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton2)
                            .addComponent(jButton3)
                            .addComponent(jButton1))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration                   
}
